//package com.jiangsong.batch;
//
//
//import com.jiangsong.entity.Product;
//import org.springframework.batch.core.Job;
//import org.springframework.batch.core.Step;
//import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
//import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
//import org.springframework.batch.item.ItemWriter;
//import org.springframework.batch.item.database.JdbcPagingItemReader;
//import org.springframework.batch.item.database.JpaItemWriter;
//import org.springframework.batch.item.database.Order;
//import org.springframework.batch.item.database.builder.JdbcPagingItemReaderBuilder;
//import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.jdbc.core.BeanPropertyRowMapper;
//
//import javax.persistence.EntityManagerFactory;
//import javax.sql.DataSource;
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.Map;
//
//@Configuration
//public class BatchConfig2 {
//    private final JobBuilderFactory jobBuilderFactory;
//    private final StepBuilderFactory stepBuilderFactory;
//
//    @Autowired
//    private DataSource dataSource; // 假设已经配置了 DataSource
//
//
//    public BatchConfig2(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
//        this.jobBuilderFactory = jobBuilderFactory;
//        this.stepBuilderFactory = stepBuilderFactory;
//    }
//
//    @Bean
//    public JdbcPagingItemReader<Product> jdbcPagingItemReader() {
//        Map<String, Order> sortKeys = new HashMap<>();
//        sortKeys.put("acc_no", Order.DESCENDING); // 排序字段和排序顺序
//
//        MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
//        queryProvider.setSelectClause("SELECT distinct acc_no");
//        queryProvider.setFromClause("FROM product");
//        queryProvider.setWhereClause("WHERE valid_flag = :validFlag"); // 自定义 WHERE 子句
//        queryProvider.setSortKeys(sortKeys);
//
//        return new JdbcPagingItemReaderBuilder<Product>()
//                .name("jdbcPagingItemReader")
//                .dataSource(dataSource)
////                .fetchSize(10) // 每次读取的记录数
////                .rowMapper(new ProductRowMapper())
//                .rowMapper(new BeanPropertyRowMapper<>(Product.class))
//                .queryProvider(queryProvider)
//                .parameterValues(Collections.singletonMap("validFlag", "1")) // 自定义参数
//                .pageSize(10) // 分页大小
//                .build();
//    }
//
//    @Bean
//    public JpaItemWriter<Product> writer(EntityManagerFactory entityManagerFactory) {
//        JpaItemWriter<Product> writer = new JpaItemWriter<>();
//        writer.setEntityManagerFactory(entityManagerFactory);
//        return writer;
//    }
//
//    @Bean
//    public ItemWriter<Product> itemWriter() {
//        return items -> {
////            for (MyObject item : items) {
////                System.out.println(item);
////            }
//        };
//    }
//
//    @Bean
//    public Step step1(ProductItemProcessor processor, JpaItemWriter<Product> writer) {
//        return stepBuilderFactory.get("step1")
//                .<Product, Product>chunk(5)
//                .reader(jdbcPagingItemReader())
//                .processor(new ProductItemProcessor())
//                .writer(itemWriter())
////                .listener(productChunkListener)
//                .build();
//    }
//
//    @Bean
//    public Job job(Step step1) {
//        return jobBuilderFactory.get("job")
//                .start(step1)
//                .build();
//    }
//}